﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Diagnostics;


namespace Project_CPU.GenerateData
{
    public class Generator
    {
        private static List<Data> statistics = new List<Data>();

        public struct Data
        {
            public DateTime date;
            public int value;

            public Data(DateTime date, int value)
            {
                this.date = date;
                this.value = value;
            }
        }

        public static List<Data> ReturnStatistics()
        {
            return statistics;
        }

        public static void Generate()
        {
            Thread t = new Thread(Go);
            Random rand = new Random(DateTime.Now.Second);
            var finish = DateTime.Now.AddSeconds(3);

            for (var i = new DateTime(2014, 1, 1, 0, 0, 0); i < finish; i = i.AddSeconds(1))
            {
                statistics.Add(new Data(i, rand.Next(0, 100)));
            }

            t.Start();
        }

        private static void Go()
        {
            PerformanceCounter theCPUCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total");
            //Random rand = new Random(DateTime.Now.Second);
            while (true)
            {
                System.Threading.Thread.Sleep(1000);
                statistics.Add(new Data(DateTime.Now, (int)theCPUCounter.NextValue()));
                //statistics.Add(new Data(DateTime.Now, rand.Next(0, 100)));
            }
        }
    }
}